<?php

class User_Model extends TinyMVC_Model
{
	protected $_data = array();
	
	
	function find($userIdOrName) {
		if(is_numeric($userIdOrName)) {
			$query = "select * from users where user_id=" . $userIdOrName . " limit 1";
		} else {
			$query = "select * from users where name='" . addslashes($userIdOrName) . "' limit 1";
		}
		$this->db->query($query);
		if($row = $this->db->next()) {
			$this->_data = $row;
			return $this;
		} else {
			return null;
		}
	}
	
	private $passwordSuffix = "12";

	function isValidPassword($password)
	{
		if(empty($this->_data)) {
			throw new Exception('No user data fetched');
		}
		return $this->_data['password'] == md5($password .$this->passwordSuffix);
	}

	
	function getEmail() {
		return $this->_data['email'];
	}
	
	function getId() {
		return $this->_data['user_id'];
	}
	
	function getName()
	{
		return $this->_data['name'];
	}
}